package com.emapp.lccode.activity

import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.os.Bundle
import android.widget.DatePicker
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ProgressAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.databinding.ActivityProgressListBinding
import com.emapp.lccode.mode.ProjectDeviceViewModel
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.toEntity
import com.emapp.lccode.utils.toJsonStr
import com.emapp.lccode.utils.vertical
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**项目
 */
class ProgressListActivity : BaseActivity1<ProjectDeviceViewModel, ActivityProgressListBinding>(),
    OnDateSetListener, ProgressAdapter.OnButtonClickListener {
    var datas: ArrayList<DeviceKt> = arrayListOf()
    private val progressAdapter: ProgressAdapter by lazy { ProgressAdapter(this) }
    var page = 1
    lateinit var infor: DeviceKt
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
       // infor = intent.getSerializableExtra("infor") as DeviceKt? ?: DeviceKt()
        val inf = MmkvUtil.getString("infor","")
        if (inf.isNotEmpty()){
            infor= inf.toEntity<DeviceKt>()!!
            MmkvUtil.putString("infor","")
        }
        progressAdapter.build_status=infor.build_status
        mViewBind.lvTitle.tvTitle.text = "工程进度"
        mViewBind.lvTitle.tvRight.text = "上报"
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            vertical()
            adapter = progressAdapter
        }
        progressAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
            }
            isEmptyViewEnable = true
            emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@ProgressListActivity))
            setRefreshFooter(ClassicsFooter(this@ProgressListActivity))
            setOnRefreshListener {
                page = 1
                getList()
            }
            setOnLoadMoreListener {
                page++
                getList()
            }
        }
        showLoading()
        getList()
    }

    private fun getList() {
        mViewModel.getProgressList(infor.id)
    }

    override fun createObserver() {
        mViewModel.run {
            progressList.observe(this@ProgressListActivity, Observer { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it != null) {
                            datas.addAll(it)
                            if (it.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it != null) {
                            datas.addAll(it)
                            if (it.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                    datas.reverse()
                    progressAdapter.submitList(datas)
                    progressAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                    mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                })
            })
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_right -> {
                    log_e(infor.toJsonStr())
                    val intent = Intent(this, ProjectReportActivity::class.java)
                    intent.putExtra("flag", "create")
                    intent.putExtra("device_id", infor.id)
                    intent.putExtra("device_name", infor.name)
                    intent.putExtra("create_time", infor.star_time)
                    intent.putExtra("project_id", infor.project_id)
                    intent.putExtra("project_name", infor.projectname)
//                    intent.putExtra("infor1", infor)
//                    intent.putExtra("infor2", infor)
//                    intent.putExtra("infor3", infor)
                    MmkvUtil.putString("infor1",infor.toJsonStr())
//                    MmkvUtil.putString("infor2",infor.toJsonStr())
//                    MmkvUtil.putString("infor3",infor.toJsonStr())

                    startActivity(intent)
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }
            EventBusConfig.REFRESH_PROJECT_DEVICE -> {
                page = 1
                getList()
            }
            else -> {}
        }
    }


    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
    }

    override fun onSee(position: Int) {
        val intent = Intent(this, ProgressInfoActivity::class.java)
        intent.putExtra("id", datas[position].id)
        startActivity(intent)
    }

    override fun onEdit(position: Int) {
        val intent = Intent(this, ProgressEditActivity::class.java)
        intent.putExtra("progress_id", datas[position].id)
        intent.putExtra("create_time", infor.star_time)
        startActivity(intent)
    }

}
